package com.jeeplus.test.jlnu.graduationInfo.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.test.jlnu.archivalInfo.domain.entity.ArchParmerter;
import com.jeeplus.test.jlnu.fieldConfig.mapper.GraduationFieldConfigMapper;
import com.jeeplus.test.jlnu.graduationInfo.domain.entity.GraduationPameter;
import com.jeeplus.test.jlnu.graduationInfo.domain.vo.GraduationInfoReqVo;
import com.jeeplus.test.jlnu.graduationInfo.mapper.GraduationInfoMapper;
import com.jeeplus.test.jlnu.graduationInfo.service.GraduationInfoService;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class GraduationInfoServiceImpl implements GraduationInfoService {

    @Autowired
    private GraduationInfoMapper graduationInfoMapper;

    @Autowired
    private GraduationFieldConfigMapper graduationFieldConfigMapper;

    @Override
    public Page<GraduationInfoReqVo> getGraduationInfoPage(ArchParmerter graduationInfoReqVo, Page<GraduationInfoReqVo> page) {
        // 1. 获取分页结果和字段配置信息
        Page<GraduationInfoReqVo> graduationInfoPage = graduationInfoMapper.getGraduationInfoPage(graduationInfoReqVo, page);

        return graduationInfoPage;
    }

    @Override
    public int searchIn(String name, String idCardNum) {
        return graduationInfoMapper.searchIn(name,idCardNum);
    }

    @Override
    public Map<String, String> internalQueries(GraduationPameter graduationPameter) throws BadHanyuPinyinOutputFormatCombination {
        //先根据学号和姓名在库里查全部信息，很多关联表
        GraduationPameter graduationPameter1 = graduationInfoMapper.selectAll(graduationPameter.getStudentId(), graduationPameter.getName());

        Map<String, String> comparisonResults = new HashMap<>();

        // 判断并比较字段
        comparisonResults.put("studentId", graduationPameter.getStudentId() == null ? "未提供" :
                graduationPameter.getStudentId().equals(graduationPameter1.getStudentId()) ? "一致" : "不一致");
        comparisonResults.put("admissionMajor", graduationPameter.getAdmissionMajor() == null ? "未提供" :
                graduationPameter.getAdmissionMajor().equals(graduationPameter1.getAdmissionMajor()) ? "一致" : "不一致");

        String name = graduationPameter1.getName();
        String inName = graduationPameter.getName();

        String pinyin1 = getPinyin(name);
        String pinyin2 = getPinyin(inName);
        if (name == null || inName == null) {
            comparisonResults.put("name", "未提供");
        } else if (pinyin1.equals(pinyin2)) {
            // 拼音一致，但名字不同
            if (name.equals(inName)) {
                comparisonResults.put("name", "一致");
            } else {
                comparisonResults.put("name", "拼音一致，但文字不完全一致");
            }
        } else {
            // 拼音不一致
            comparisonResults.put("name", "不一致");
        }
/*

        comparisonResults.put("name", graduationPameter.getName() == null ? "未提供" :
                graduationPameter.getName().equals(graduationPameter1.getName()) ? "一致" : "不一致");*/

        comparisonResults.put("gender", graduationPameter.getGender() == null ? "未提供" :
                graduationPameter.getGender().equals(graduationPameter1.getGender()) ? "一致" : "不一致");

        comparisonResults.put("nation", graduationPameter.getNation() == null ? "未提供" :
                graduationPameter.getNation().equals(graduationPameter1.getNation()) ? "一致" : "不一致");

        comparisonResults.put("nativePlace", graduationPameter.getNativePlace() == null ? "未提供" :
                graduationPameter.getNativePlace().equals(graduationPameter1.getNativePlace()) ? "一致" : "不一致");

        comparisonResults.put("birthday", graduationPameter.getBirthday() == null ? "未提供" :
                graduationPameter.getBirthday().equals(graduationPameter1.getBirthday()) ? "一致" : "不一致");

        comparisonResults.put("studyStartTime", graduationPameter.getStudyStartTime() == null ? "未提供" :
                graduationPameter.getStudyStartTime().equals(graduationPameter1.getStudyStartTime()) ? "一致" : "不一致");

        comparisonResults.put("studyEndTime", graduationPameter.getStudyEndTime() == null ? "未提供" :
                graduationPameter.getStudyEndTime().equals(graduationPameter1.getStudyEndTime()) ? "一致" : "不一致");

        comparisonResults.put("school", graduationPameter.getSchool() == null ? "未提供" :
                graduationPameter.getSchool().equals(graduationPameter1.getSchool()) ? "一致" : "不一致");

        comparisonResults.put("educationalSystem", graduationPameter.getEducationalSystem() == null ? "未提供" :
                graduationPameter.getEducationalSystem().equals(graduationPameter1.getEducationalSystem()) ? "一致" : "不一致");

        comparisonResults.put("gradation", graduationPameter.getGradation() == null ? "未提供" :
                graduationPameter.getGradation().equals(graduationPameter1.getGradation()) ? "一致" : "不一致");

        comparisonResults.put("graduationDate", graduationPameter.getGraduationDate() == null ? "未提供" :
                graduationPameter.getGraduationDate().equals(graduationPameter1.getGraduationDate()) ? "一致" : "不一致");

        comparisonResults.put("diplomaNum", graduationPameter.getDiplomaNum() == null ? "未提供" :
                graduationPameter.getDiplomaNum().equals(graduationPameter1.getDiplomaNum()) ? "一致" : "不一致");

        comparisonResults.put("degreeLevel", graduationPameter.getDegreeLevel() == null ? "未提供" :
                graduationPameter.getDegreeLevel().equals(graduationPameter1.getDegreeLevel()) ? "一致" : "不一致");

        comparisonResults.put("degreeType", graduationPameter.getDegreeType() == null ? "未提供" :
                graduationPameter.getDegreeType().equals(graduationPameter1.getDegreeType()) ? "一致" : "不一致");

        comparisonResults.put("degreeCertificateNum", graduationPameter.getDegreeCertificateNum() == null ? "未提供" :
                graduationPameter.getDegreeCertificateNum().equals(graduationPameter1.getDegreeCertificateNum()) ? "一致" : "不一致");

        return comparisonResults;
    }

    @Override
    public Map<String, String> selectAll(String studentId, String name) {
        //先根据学号和姓名在库里查全部信息，很多关联表
        GraduationPameter graduationPameter = new GraduationPameter();
        graduationPameter.setStudentId(studentId);
        graduationPameter.setName(name);
        GraduationPameter graduationPameter1 = graduationInfoMapper.selectAll(graduationPameter.getStudentId(), graduationPameter.getName());

        Map<String, String> comparisonResults = new HashMap<>();

        // 判断并比较字段
        comparisonResults.put("studentId", graduationPameter.getStudentId() == null ? "未提供" :
                graduationPameter.getStudentId().equals(graduationPameter1.getStudentId()) ? "一致" : "不一致");
        comparisonResults.put("admissionMajor", graduationPameter.getAdmissionMajor() == null ? "未提供" :
                graduationPameter.getAdmissionMajor().equals(graduationPameter1.getAdmissionMajor()) ? "一致" : "不一致");

        comparisonResults.put("name", graduationPameter.getName() == null ? "未提供" :
                graduationPameter.getName().equals(graduationPameter1.getName()) ? "一致" : "不一致");

        comparisonResults.put("gender", graduationPameter.getGender() == null ? "未提供" :
                graduationPameter.getGender().equals(graduationPameter1.getGender()) ? "一致" : "不一致");

        comparisonResults.put("nation", graduationPameter.getNation() == null ? "未提供" :
                graduationPameter.getNation().equals(graduationPameter1.getNation()) ? "一致" : "不一致");

        comparisonResults.put("nativePlace", graduationPameter.getNativePlace() == null ? "未提供" :
                graduationPameter.getNativePlace().equals(graduationPameter1.getNativePlace()) ? "一致" : "不一致");

        comparisonResults.put("birthday", graduationPameter.getBirthday() == null ? "未提供" :
                graduationPameter.getBirthday().equals(graduationPameter1.getBirthday()) ? "一致" : "不一致");

        comparisonResults.put("studyStartTime", graduationPameter.getStudyStartTime() == null ? "未提供" :
                graduationPameter.getStudyStartTime().equals(graduationPameter1.getStudyStartTime()) ? "一致" : "不一致");

        comparisonResults.put("studyEndTime", graduationPameter.getStudyEndTime() == null ? "未提供" :
                graduationPameter.getStudyEndTime().equals(graduationPameter1.getStudyEndTime()) ? "一致" : "不一致");

        comparisonResults.put("school", graduationPameter.getSchool() == null ? "未提供" :
                graduationPameter.getSchool().equals(graduationPameter1.getSchool()) ? "一致" : "不一致");

        comparisonResults.put("educationalSystem", graduationPameter.getEducationalSystem() == null ? "未提供" :
                graduationPameter.getEducationalSystem().equals(graduationPameter1.getEducationalSystem()) ? "一致" : "不一致");

        comparisonResults.put("gradation", graduationPameter.getGradation() == null ? "未提供" :
                graduationPameter.getGradation().equals(graduationPameter1.getGradation()) ? "一致" : "不一致");

        comparisonResults.put("graduationDate", graduationPameter.getGraduationDate() == null ? "未提供" :
                graduationPameter.getGraduationDate().equals(graduationPameter1.getGraduationDate()) ? "一致" : "不一致");

        comparisonResults.put("diplomaNum", graduationPameter.getDiplomaNum() == null ? "未提供" :
                graduationPameter.getDiplomaNum().equals(graduationPameter1.getDiplomaNum()) ? "一致" : "不一致");

        comparisonResults.put("degreeLevel", graduationPameter.getDegreeLevel() == null ? "未提供" :
                graduationPameter.getDegreeLevel().equals(graduationPameter1.getDegreeLevel()) ? "一致" : "不一致");

        comparisonResults.put("degreeType", graduationPameter.getDegreeType() == null ? "未提供" :
                graduationPameter.getDegreeType().equals(graduationPameter1.getDegreeType()) ? "一致" : "不一致");

        comparisonResults.put("degreeCertificateNum", graduationPameter.getDegreeCertificateNum() == null ? "未提供" :
                graduationPameter.getDegreeCertificateNum().equals(graduationPameter1.getDegreeCertificateNum()) ? "一致" : "不一致");

        return comparisonResults;
    }

    @Override
    public GraduationPameter selectAllByStuId(String studentId, String name) {
        GraduationPameter graduationPameter1 = graduationInfoMapper.selectAll(studentId,name);
        return graduationPameter1;
    }

    @Override
    public int addGraduationInfo(GraduationInfoReqVo graduationInfoReqVo) {
        return graduationInfoMapper.addGraduationInfo(graduationInfoReqVo);
    }

    @Override
    public int updateGraduation(GraduationInfoReqVo graduationInfoReqVo) {
       return graduationInfoMapper.updateGraduation(graduationInfoReqVo);
    }

    /**
     * 获取中文字符串的拼音
     */
    public static String getPinyin(String chineseString) throws BadHanyuPinyinOutputFormatCombination {
        if (chineseString == null) return "";

        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);  // 小写拼音
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE); // 无音调

        StringBuilder pinyinBuilder = new StringBuilder();
        for (int i = 0; i < chineseString.length(); i++) {
            char character = chineseString.charAt(i);
            // 如果是汉字，则转换为拼音
            if (Character.toString(character).matches("[\\u4e00-\\u9fa5]")) {
                String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(character, format);
                if (pinyinArray != null) {
                    pinyinBuilder.append(pinyinArray[0]); // 取第一个拼音（汉字有多音时）
                }
            } else {
                pinyinBuilder.append(character); // 非汉字字符直接添加
            }
        }

        return pinyinBuilder.toString();
    }
}

